package trangchu;


import hethong.DoiMatKhau;
import hethong.ThemNguoiDung;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import chucnang.BanHang;
import chucnang.ButtonTabComponent;
import chucnang.ChuyenKho;
import chucnang.MuaHang;
import chucnang.ThuTien;
import chucnang.TonKho;
import dangnhap.FromDangNhap;

public class HeThong extends JPanel implements ActionListener {
	private JToolBar tbr;
	private JButton btnDangXuat,btnPhanQuyen,btnQuanLy,btnThongTin,btnDoiMK,btnKetThuc;
	private TrangChu trangchu;
	private DoiMatKhau doimk;
	private FromDangNhap dangnhap;
	private ThemNguoiDung themnguoidung;
	public HeThong(TrangChu trangchu){
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.trangchu = trangchu;
		this.tbr = new JToolBar();
		this.add(this.tbr);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnDangXuat = new JButton("Đăng Xuất",new ImageIcon(FromDangNhap.rootPath+"/images/dangxuat.gif"));
		this.btnDangXuat.setVerticalTextPosition(JButton.BOTTOM);
		this.btnDangXuat.setHorizontalTextPosition(JButton.CENTER);		
		this.tbr.add(this.btnDangXuat);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnPhanQuyen = new JButton("Phân Quyền",new ImageIcon(FromDangNhap.rootPath+"images/phanquyen.gif"));
		this.btnPhanQuyen.setVerticalTextPosition(JButton.BOTTOM);
		this.btnPhanQuyen.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnPhanQuyen);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnQuanLy = new JButton("Quản Lý",new ImageIcon(FromDangNhap.rootPath+"images/quanly.gif"));
		this.btnQuanLy.setVerticalTextPosition(JButton.BOTTOM);
		this.btnQuanLy.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnQuanLy);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnThongTin = new JButton("Thông Tin",new ImageIcon(FromDangNhap.rootPath+"images/thongtinht.gif"));
		this.btnThongTin.setVerticalTextPosition(JButton.BOTTOM);
		this.btnThongTin.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnThongTin);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnDoiMK = new JButton("Đổi Mật Khẩu",new ImageIcon(FromDangNhap.rootPath+"images/doimk.gif"));
		this.btnDoiMK.setVerticalTextPosition(JButton.BOTTOM);
		this.btnDoiMK.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnDoiMK);		
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnKetThuc = new JButton("Kết Thúc",new ImageIcon(FromDangNhap.rootPath+"images/ketthucht.gif"));
		this.btnKetThuc.setVerticalTextPosition(JButton.BOTTOM);
		this.btnKetThuc.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnKetThuc);
				
		
		// su kien
		this.btnDangXuat.addActionListener(this);
		this.btnThongTin.addActionListener(this);
		this.btnPhanQuyen.addActionListener(this);
		this.btnQuanLy.addActionListener(this);
		this.btnDoiMK.addActionListener(this);
		this.btnKetThuc.addActionListener(this);
		doimk = new DoiMatKhau(trangchu);
		dangnhap = new FromDangNhap();
		themnguoidung = new ThemNguoiDung();
		
	}
	public static Object currentTab;
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()== this.btnDangXuat){				
			dangnhap.setVisible(true);
		}
		
		if(e.getSource()== this.btnPhanQuyen){
			int index = this.trangchu.tbnPaneCenter.positionTab("Phân Quyền");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new ChuyenKho(trangchu), "Phân Quyền");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btnQuanLy){				
			themnguoidung.setVisible(true);
		}
		if(e.getSource()== this.btnThongTin){

			int index = this.trangchu.tbnPaneCenter.positionTab("Thông Tin");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new BanHang(trangchu), "Thông Tin");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
			
		}
		if(e.getSource()== this.btnDoiMK){
			doimk.setVisible(true);
		}
		
		if(e.getSource()== this.btnKetThuc){
			System.exit(0);
		}
		
		int count = this.trangchu.tbnPaneCenter.getTabCount();
		for(int i=0;i<count;i++){
			this.trangchu.tbnPaneCenter.setTabComponentAt(i, new ButtonTabComponent(this.trangchu.tbnPaneCenter));
			
		}
		//HeThong.currentTab = this.trangchu.tbnPaneCenter.getComponentAt(this.trangchu.tbnPaneCenter.getSelectedIndex());
		
	}
}

